package cjgl.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cjgl.entity.Course;
import cjgl.entity.Grade;
import cjgl.entity.Student;
import cjgl.entity.TbClass;
import cjgl.service.CourseService;
import cjgl.service.GradeService;
import cjgl.service.StudentService;
import cjgl.service.TbClassService;

/**
 * Servlet implementation class GradeServlet
 */
@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private TbClassService classService = new TbClassService();   
    private StudentService studentService = new StudentService();
    private GradeService gradeService = new GradeService();
    private CourseService courseService = new CourseService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if("peasonGrade".equals(method)) {
			peasonGrade(request,response);
		}else if("xs".equals(method)) {
			xs(request,response);
		}else if("cx".equals(method)) {
			cx(request,response);
		}else if("xg".equals(method)) {
			xg(request,response);
		}else if("classGrade".equals(method)) {
			classGrade(request,response);
		}else if("kcx".equals(method)) {
			kcx(request,response);
		}else if("allstugrade".equals(method)){
			allstugrade(request,response);
		}else if("bjSel".equals(method)){
			bjSel(request,response);
		}else if("myGrade".equals(method)) {
			myGrade(request,response);
		}else if("myClassGrade".equals(method)) {
			myClassGrade(request,response);
		}else if("xsfh".equals(method)) {
			xsfh(request,response);
		}
	}

	private void xsfh(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("jsp/main/stumain2.jsp").forward(request, response);
	}

	private void myClassGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student stu = (Student)session.getAttribute("user");
		Integer bjId = stu.getTbclass().getId();
		List<Course> courses=gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId,courses);
		request.setAttribute("kcs",courses);
		request.setAttribute("cjb",cjb);
		request.getRequestDispatcher("jsp/grade/cjb2.jsp").forward(request, response);
	}

	private void myGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student stu =(Student)session.getAttribute("user");
		List<Grade> grade = gradeService.findByStuId(stu.getId());
		request.setAttribute("grade", grade);
		request.getRequestDispatcher("jsp/grade/mygrade.jsp").forward(request, response);
	}

	private void bjSel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId = Integer.parseInt(request.getParameter("bjId"));
		List<Course> courses=gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId,courses);
		request.setAttribute("kcs",courses);
		request.setAttribute("cjb",cjb);
		request.getRequestDispatcher("jsp/grade/cjb.jsp").forward(request, response);
	}

	private void allstugrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> list = classService.findAll();
		request.setAttribute("list",list);
		request.getRequestDispatcher("jsp/grade/allstugrade.jsp").forward(request, response);
	}

	private void kcx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId = Integer.parseInt(request.getParameter("bjId"));
		Integer kcId = Integer.parseInt(request.getParameter("kcId"));
		List<Student> students = studentService.findByTbClassId(bjId);
		List<Grade> grades = new ArrayList<Grade>();
		for (Student student : students) {
			Grade grade = gradeService.findByStuIdAndKcId(student.getId(),kcId);
			if(grade==null) {
				grade = new Grade(null,student.getId(),kcId, null);
				gradeService.save(grade);
			}
			grades.add(grade);
		}
		request.setAttribute("grades", grades);
		request.getRequestDispatcher("jsp/grade/classtable.jsp").forward(request, response);
			
	}
		
	

	private void classGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Course> kcs = courseService.findAll();
		List<TbClass> bjs = classService.findAll();
		request.setAttribute("bjs", bjs);
		request.setAttribute("kcs", kcs);
		request.getRequestDispatcher("jsp/grade/classGrade.jsp").forward(request, response);
	}

	private void xg(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Integer cj=Integer.parseInt(request.getParameter("grade"));
		Grade grade = gradeService.findById(id);
		grade.setGrade(cj);
		gradeService.save(grade);
		PrintWriter out= response.getWriter();
		out.print("ok");
		out.close();
	}

	private void cx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer stuId = Integer.parseInt(request.getParameter("stuId"));
		List<Grade> cjlist =gradeService.findByStuId(stuId);
		request.setAttribute("cjlist",cjlist);
		request.getRequestDispatcher("jsp/grade/table.jsp").forward(request, response);
	}

	private void xs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId  = Integer.parseInt(request.getParameter("bjId"));
		List<Student> list = studentService.findByTbClassId(bjId);
		request.setAttribute("xslist", list);
		request.getRequestDispatcher("jsp/grade/xs.jsp").forward(request, response);
	}

	private void peasonGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> list = classService.findAll();
		request.setAttribute("list",list);
		request.getRequestDispatcher("jsp/grade/peasonGrade.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
